package com.moss.cloud.auth.biz.service.impl;

import com.moss.cloud.auth.biz.service.IAuthorizationService;
import com.moss.cloud.common.core.constant.Constant;
import com.moss.cloud.common.security.component.RedisTemplateOps;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

/**
 * 用户授权成功、失败业务处理实现
 *
 * @author 瑾年
 * @data 2023年3月6日
 */
@Service
public class AuthorizationServiceImpl implements IAuthorizationService {
    @Resource
    private RedisTemplateOps redisTemplateOps;

    /**
     * redis中记录当前登录用户登录错误次数
     * redis存储纪录保持10分钟，即登录错误超过5次账户锁定10分钟
     *
     * @param userName
     */
    @Override
    public void recordFailNum(String userName) {
        redisTemplateOps.incr(Constant.LOGIN_NUM + userName, 1);
        redisTemplateOps.expire(Constant.LOGIN_NUM + userName, 600);
    }

    /**
     * 限制用户登录
     *
     * @param username
     * @return
     */
    public Boolean isBlocked(String username) {
        Boolean hasKey = redisTemplateOps.hasKey(Constant.LOGIN_NUM + username);
        return hasKey ? (int) redisTemplateOps.get(Constant.LOGIN_NUM + username) >= 5 : Boolean.FALSE;
    }


}
